package com.rjwh.dingdong.client.mvp.presenter.impl;

import android.content.Context;

import com.aishua.lib.task.AsResult;
import com.aishua.lib.utils.AsStrUtils;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.bean.jsonbean.ResGetMyMainPageBean;
import com.rjwh.dingdong.client.bean.localbean.MyMainPageBeanData;
import com.rjwh.dingdong.client.bean.localbean.TopicData;
import com.rjwh.dingdong.client.constant.NetConstant;
import com.rjwh.dingdong.client.mvp.BasePresenter;
import com.rjwh.dingdong.client.mvp.model.impl.CommunityPostMyMainPageModel;
import com.rjwh.dingdong.client.mvp.presenter.ICommunityPostMyMainPagePresenter;
import com.rjwh.dingdong.client.mvp.view.CommunityPostMyMainPageView;
import com.rjwh.dingdong.client.util.ToastUtil;

import java.util.List;

/**
 * @author Kay
 * @ClassName CommunityPostMyMainPagePresenter
 * @Description
 * @date 2016-7-29
 * @history 2016-7-29 author: description:
 */
public class CommunityPostMyMainPagePresenter extends BasePresenter<CommunityPostMyMainPageView> implements ICommunityPostMyMainPagePresenter {
    private CommunityPostMyMainPageModel model;
    private SwipyRefreshLayoutDirection mDirection;

    public CommunityPostMyMainPagePresenter(Context context, CommunityPostMyMainPageView mMvpView) {
        super(context, mMvpView);
        model = new CommunityPostMyMainPageModel(this);
    }

    @Override
    public void onServerDataAcquired(int businessType, AsResult mAsResult, boolean isRefresh) {
        if (isViewAttached()) {
            switch (businessType) {
                case NetConstant.COMMUNITY_GETMYMAINPAGE:
                    getMvpView().setRefreshing(false);
                    ResGetMyMainPageBean resGetMyMainPageBean = (ResGetMyMainPageBean) mAsResult.getObj();
                    if (mAsResult.getResultCode() > NetConstant.MSG_CONNECT_SUCCESS && resGetMyMainPageBean != null) {
                        MyMainPageBeanData data = resGetMyMainPageBean.getData();
                        if (data != null) {
                            List<TopicData> topicilsit = data.getTopiclist();
                            if (topicilsit != null) {
                                if (topicilsit.isEmpty()) {
                                    getMvpView().setDirection(SwipyRefreshLayoutDirection.TOP);
                                    if (mDirection == SwipyRefreshLayoutDirection.BOTTOM) {
                                        ToastUtil.showToast(getContext(), getContext().getString(R.string.empty_data_more));
                                    } else {
                                        ToastUtil.showToast(getContext(), getContext().getString(R.string.empty_data));
                                    }
                                } else {
                                    getMvpView().setDirection(SwipyRefreshLayoutDirection.BOTH);
                                }
                                getMvpView().loadData(isRefresh, topicilsit);
                            }
                        }
                    } else {
                        getMvpView().setDirection(SwipyRefreshLayoutDirection.TOP);
                        ToastUtil.showToast(getContext(), getContext().getString(R.string.refresh_error));
                    }
                    break;

                default:
                    break;
            }
        }
    }

    @Override
    public void detachView() {
        detachViews();
        model.detachView();
    }

    /**
     * 获取个人主页
     *
     * @param posterid   发帖人id
     * @param pageNumber 页码
     * @param isRefresh  是否刷新
     * @param direction
     */
    @Override
    public void doGetMyMainPage(String posterid, int pageNumber, boolean isRefresh, SwipyRefreshLayoutDirection direction) {
        mDirection = direction;
        model.doGetMyMainPage(posterid, pageNumber, isRefresh);
    }
}
